/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.account;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.account.SysRole;
import com.unis.service.account.SysRoleService;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-12-06
 */
@Controller
@RequestMapping("/sysrole")
public class SysRoleController {

    private SysRoleService sysroleService;

    /****************************************
     * Insert a instace of SysRole
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertSysRole(@RequestBody SysRole sysrole) throws Exception {
        sysrole.setStatmentId("SysRole.insert");
        String status = "failure";
        sysroleService.insert(sysrole);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of SysRole
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody SysRole sysrole) throws Exception {
        sysrole.setStatmentId("SysRole.update");
        String status = "failure";
        sysroleService.update(sysrole);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of SysRole
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody SysRole sysrole) throws Exception {
        sysrole.setStatmentId("SysRole.delete");
        String status = "failure";
        sysroleService.batchDelete(sysrole);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of SysRole
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        SysRole sysrole = new SysRole();
        sysrole.setStatmentId("SysRole.queryAsList");
        BeanUtils.populate(sysrole, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(sysrole, pageMap);
        List<BaseModel> sysroleList = sysroleService.queryAsList(sysrole);
        Map<String, Object> renderMap = GridUIMapper.render(sysrole, sysroleList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of SysRole
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody SysRole sysrole) throws Exception {
        sysrole.setStatmentId("SysRole.queryAsObject");
        return sysroleService.queryAsObject(sysrole);
    }

    @RequestMapping("/roleMgr")
    public String roleMgr() {
        return "sys/sysroleList";
    }

    @Resource
    public void setSysRoleService(SysRoleService sysroleService) {
        this.sysroleService = sysroleService;
    }
}
